﻿using System;
using System.ComponentModel;
using System.Web.UI;
using System.Collections;
using System.Collections.Generic;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.Core;
using System.Web;

namespace Yuanlin.Business
{
    public class Navigation : YuanlinUserControl
    {
        private string _selected = null;
        private bool _reverse = false;
        protected Repeater menu;
        protected Repeater menuHuamu;
        protected Repeater menuZiCai;
        protected Repeater menuNews;
        protected Repeater menuService;
        private ArrayList filterMenu = new ArrayList();
        private string greenNum = UrlManager.Instance.GetParameterValue<string>(YuanlinParameterName.GreenNum);

        public string Selected
        {
            get
            {
                if (_selected == null)
                    _selected = Context.Items["SelectedNavigation"] as string;

                return _selected;
            }
            set
            {
                _selected = value;
            }
        }
        public bool Reverse
        {
            get { return _reverse; }
            set { _reverse = value; }
        }

        protected override void OnLoad(EventArgs e)
        {
            menu.ItemDataBound += new RepeaterItemEventHandler(Menu_ItemDataBound);
            menuHuamu.ItemDataBound += new RepeaterItemEventHandler(Menu_ItemDataBound);
            menuZiCai.ItemDataBound += new RepeaterItemEventHandler(Menu_ItemDataBound);
            menuNews.ItemDataBound += new RepeaterItemEventHandler(Menu_ItemDataBound);
            menuService.ItemDataBound += new RepeaterItemEventHandler(Menu_ItemDataBound);
            DataBind();
        }
        public override void DataBind()
        {
            base.DataBind();

            string cacheKey = this.GetType().ToString();

            ArrayList links = CacheManager.Get(cacheKey) as ArrayList;
            ArrayList filterLinks = new ArrayList();
            ArrayList filterLinkHuamus = new ArrayList();
            ArrayList filterLinkZiCais = new ArrayList();
            ArrayList filterLinkNews = new ArrayList();
            ArrayList filterLinkServices = new ArrayList();

            if (links == null)
            {
                links = SiteUrls.Instance().TabCompanyUrls;
                CacheManager.Insert(cacheKey, links, CacheManager.MinuteFactor * 15);
            }

            CompanyGreenNumListRequest request = new CompanyGreenNumListRequest();
            request.GreenNum = greenNum;
            EntityList companyGreenNums = BusinessManager.GetCompanyGreenNumList(RequestHelper.BuildGetCompanyGreenNumListRequest(request));
            if (companyGreenNums.Count == 0)
            {
                return;
            }
            YuanlinUser user = UserManager.GetUser((companyGreenNums[0] as CompanyGreenNum).UserId.Value) as YuanlinUser;
            if (user == null)
            {
                return;
            }

            if (CurrentUser.IsAnonymous || CurrentUser.EntityId.Value != UserIdFromGreenNum)
            {
                filterMenu.Add("Business");
                filterMenu.Add("CompanyFriend");
                filterMenu.Add("CompanyVisitor");
            }

            foreach (NavLink link in links)
            {
                //EntityList roleList = CurrentUser.RoleList;
                //bool authorized = false;
                //foreach (Role role in roleList)
                //{
                //    if (link.Roles.ToLower().IndexOf(role.Name.Value.Trim().ToLower()) >= 0)
                //    {
                //        authorized = true;
                //        break;
                //    }
                //}
                //if (authorized == false)
                //{
                //    continue;
                //}
                if (!IsVisibleMenu(user, link.Name))
                {
                    continue;
                }
                //不是本公司登录时，过滤掉行业新闻、我的好友
                if ((CurrentUser.IsAnonymous || CurrentUser.EntityId.Value != UserIdFromGreenNum) && filterMenu.Contains(link.Name))
                {
                    continue;
                }

                if (link.NavType == "Home")
                {
                    filterLinks.Add(link);
                }
                else if (link.NavType == "HuaMu")
                {
                    filterLinkHuamus.Add(link);
                }
                else if (link.NavType == "ZiCai")
                {
                    filterLinkZiCais.Add(link);
                }
                else if (link.NavType == "News")
                {
                    filterLinkNews.Add(link);
                }
                else if (link.NavType == "Service")
                {
                    filterLinkServices.Add(link);
                }
            }

            if (Reverse)
            {
                filterLinks = filterLinks.Clone() as ArrayList;
                filterLinks.Reverse();
            }

            Context.Items["MenuNum"] = filterLinks.Count.ToString();

            menu.DataSource = filterLinks;
            menu.DataBind();
            menuHuamu.DataSource = filterLinkHuamus;
            menuHuamu.DataBind();
            menuZiCai.DataSource = filterLinkZiCais;
            menuZiCai.DataBind();
            menuNews.DataSource = filterLinkNews;
            menuNews.DataBind();
            menuService.DataSource = filterLinkServices;
            menuService.DataBind();
        }
        private void Menu_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.SelectedItem)
            {
                NavLink link = e.Item.DataItem as NavLink;
                HyperLink hl = e.Item.FindControl("menuItem") as HyperLink;
                hl.NavigateUrl = link.NavigateUrl.Replace("{0}", greenNum);
                if (!string.IsNullOrEmpty(link.ResourceName))
                {
                    hl.Text = ResourceManager.GetString(link.ResourceName);
                }
                else
                {
                    hl.Text = link.Text;
                }
                if (!string.IsNullOrEmpty(link.Target))
                {
                    hl.Target = link.Target;
                }
                if (!string.IsNullOrEmpty(link.CssClass))
                {
                    hl.CssClass = link.CssClass;
                }
            }
        }
        /// <summary>
        /// 要求把没有维护过数据的菜单不显示
        /// </summary>
        /// <param name="user">当前公司</param>
        /// <param name="name">菜单value</param>
        /// <returns></returns>
        private bool IsVisibleMenu(YuanlinUser user, string name)
        {
            bool isVisible = true;
            switch (name)
            {
                case "Huamu":
                    {
                        if (string.IsNullOrEmpty(user.Huamu))
                        {
                            isVisible = false;
                        }
                    }
                    break;
                case "Huamujingpin":
                    {
                        if (string.IsNullOrEmpty(user.Huamujingpin))
                        {
                            isVisible = false;
                        }
                    }
                    break;
                case "Huamuqiugou":
                    {
                        if (string.IsNullOrEmpty(user.Huamuqiugou))
                        {
                            isVisible = false;
                        }
                    }
                    break;
                case "Huamujishou":
                    {
                        if (string.IsNullOrEmpty(user.Huamujishou))
                        {
                            isVisible = false;
                        }
                    }
                    break;
                case "Business":
                    {
                        if (string.IsNullOrEmpty(user.Business))
                        {
                            isVisible = false;
                        }
                    }
                    break;
                case "CompanyNews":
                    {
                        if (string.IsNullOrEmpty(user.CompanyNews))
                        {
                            isVisible = false;
                        }
                    }
                    break;
                case "Certificate":
                    {
                        if (string.IsNullOrEmpty(user.Certificate))
                        {
                            isVisible = false;
                        }
                    }
                    break;
                case "CompanyShop":
                    {
                        if (string.IsNullOrEmpty(user.CompanyShop))
                        {
                            isVisible = false;
                        }
                    }
                    break;
                case "MyShopping":
                    {
                        if (string.IsNullOrEmpty(user.MyShopping))
                        {
                            isVisible = false;
                        }
                    }
                    break;
                case "CompanyBrand":
                    {
                        if (string.IsNullOrEmpty(user.CompanyBrand))
                        {
                            isVisible = false;
                        }
                    }
                    break;
                case "CompanyRecruit":
                    {
                        if (string.IsNullOrEmpty(user.CompanyRecruit))
                        {
                            isVisible = false;
                        }
                    }
                    break;
                case "CompanyAlbum":
                    {
                        if (string.IsNullOrEmpty(user.CompanyAlbum))
                        {
                            isVisible = false;
                        }
                    }
                    break;
                case "CompanyTechnology":
                    {
                        if (string.IsNullOrEmpty(user.CompanyTechnology))
                        {
                            isVisible = false;
                        }
                    }
                    break;
                case "CompanyProjectPerformance":
                    {
                        if (string.IsNullOrEmpty(user.CompanyProjectPerformance))
                        {
                            isVisible = false;
                        }
                    }
                    break;
                case "CompanyProject":
                    {
                        if (string.IsNullOrEmpty(user.CompanyProject))
                        {
                            isVisible = false;
                        }
                    }
                    break;
                case "ContactInfo":
                    {
                        if (string.IsNullOrEmpty(user.ContactInfo))
                        {
                            isVisible = false;
                        }
                    }
                    break;
                case "CompanyMachineChuShou":
                    {
                        if (string.IsNullOrEmpty(user.CompanyMachineChuShou))
                        {
                            isVisible = false;
                        }
                    }
                    break;
                case "CompanyMachineQiuGou":
                    {
                        if (string.IsNullOrEmpty(user.CompanyMachineQiuGou))
                        {
                            isVisible = false;
                        }
                    }
                    break;
                case "CompanyFacilityChuShou":
                    {
                        if (string.IsNullOrEmpty(user.CompanyFacilityChuShou))
                        {
                            isVisible = false;
                        }
                    }
                    break;
                case "CompanyFacilityQiuGou":
                    {
                        if (string.IsNullOrEmpty(user.CompanyFacilityQiuGou))
                        {
                            isVisible = false;
                        }
                    }
                    break;
                case "CompanyAgriculturalChuShou":
                    {
                        if (string.IsNullOrEmpty(user.CompanyAgriculturalChuShou))
                        {
                            isVisible = false;
                        }
                    }
                    break;
                case "CompanyAgriculturalQiuGou":
                    {
                        if (string.IsNullOrEmpty(user.CompanyAgriculturalQiuGou))
                        {
                            isVisible = false;
                        }
                    }
                    break;
                case "CompanyPartner":
                    {
                        if (string.IsNullOrEmpty(user.CompanyPartner))
                        {
                            isVisible = false;
                        }
                    }
                    break;
                case "CompanyFriend":
                    {
                        if (string.IsNullOrEmpty(user.CompanyFriend))
                        {
                            isVisible = false;
                        }
                    }
                    break;
            }
            return isVisible;
        }

    }
}
